package com.iiiss.template.ssh.impl.daos;

import java.util.List;

import org.springframework.transaction.annotation.Transactional;

import com.iiiss.template.ssh.common.impl.BaseDao;
import com.iiiss.template.ssh.core.beans.IUser;
import com.iiiss.template.ssh.core.daos.IUserDao;

/*
 * TemplateSSH - com.iiiss.template.ssh.impl.daos - UserDao.java
 * 
 * Copyright (C) 2011  Mingye Cheng <Cheng, Mingye at GMAIL>
 * 
 * UserDao.java is part of <iiiStudio Template Framework> project.
 * This project is hosted at <http://iiiss-template-ssh.googlecode.com>
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * Revision history:
 * [2011-6-1] Mingye Cheng - Initial version established.
 * [2011-9-2] Mingye Cheng - Interfaces of 'IPersistenceManager' changed,
 *                           Function 'searchUsersByUsername()' added,
 *                           Generic types adopted.
 */
@Transactional
public class UserDao extends BaseDao implements IUserDao {
	public static final String BEAN_NAME = "user";

	@SuppressWarnings("unchecked")
	public <T extends IUser> T createUser(String username, String password) {
		IUser user = persistenceManager.newObject(BEAN_NAME);
		user.setUsername(username);
		user.setPassword(password);
		return (T) user;
	}

	public <T extends IUser> T getUserByUsername(String username) {
		return persistenceManager.getObject(BEAN_NAME, "where username = ?",
				username);
	}

	public <T extends IUser> List<T> searchUsersByUsername(String keyword) {
		return persistenceManager.getObjects(BEAN_NAME,
				"where username like ?", "%" + keyword + "%");
	}
}
